﻿using System;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Liekhus.TimeTracker.Module.UnitTests
{
    [TestClass]
    public class TaskTest
    {
        private Session m_session;

        [TestInitialize]
        public void InitializeTest()
        {
            m_session = new Session();
        }

        [TestMethod]
        public void TaskCreationTest()
        {
            DateTime start = DateTime.Now;
            Task task = new Task(m_session);
            Assert.IsTrue(task.CreatedOn >= start, "Task Created On not being set corectly at initilization.");
        }

        [TestMethod]
        public void ValidTaskPersistenceTest()
        {
            DateTime start = DateTime.Now;
            Task task = new Task(m_session);
            task.Name = "Task 1";

            task.Save();
            Assert.IsTrue(task.Name.Equals("Task 1"), "Task was modified on save.");
            Assert.IsTrue(task.CreatedOn >= start, "Task Created On not being set corectly at initilization.");
            Assert.IsTrue(task.ModifiedOn >= start, "Task Modified On not being set corectly on save.");
            Assert.IsTrue(task.Status == null, "Task Status was defaulted.");
        }

        [TestMethod]
        public void InvalidTaskNameTest()
        {
            DateTime start = DateTime.Now;
            Task task = new Task(m_session);
            task.Name = string.Empty;

            RuleSet ruleSet = new RuleSet();
            RuleSetValidationResult validation = ruleSet.ValidateTarget(task, DefaultContexts.Save);

            Assert.IsTrue(validation.State==ValidationState.Invalid, "Expected to fail empty task name check.");
        }
    }
}
